#pragma once

#include <QAbstractTableModel>
#include "plotdata.h"
/*
* 用LIST 管理数据曲线
*/
class PlotTreeRootItem : public QObject {
    Q_OBJECT
    Q_PROPERTY_CREATE(QString, name);
public:
    PlotTreeRootItem() {};
    ~PlotTreeRootItem(){};

    void addData( PlotData &plot);
    void addData(QSharedPointer<QCPGraphDataContainer>& data, QString plot_name= "name");
    bool removeData(const PlotData &plot);
    void setDataStatus(const int64_t uuid, const bool status) {
        for (auto& var : _data) {
            auto UUID = var->getUUid();
            if(UUID == uuid) {
                var->setcheckStatus(status);
                return; 
            }
        }
    }
    bool getChekStatus(const QCPAxis* yAxis) const {
        if( yAxis == nullptr) return false;
		for (auto& var : _data) {
			if (var->yAxis == yAxis) {
				return var->getcheckStatus();
			}
		}
		return false;
    };
    bool hasPAxis(const QCPAxis* yAxis) const {
        if (yAxis == nullptr) return false;
        for (auto& var : _data) {
            if (var->yAxis == yAxis) {
                return true;
            }
        }
        return false;
    }

    QSharedPointer<PlotData> getPlotData(const QCPAxis* yAxis) {
        for (auto& var : _data) {
            if( var->yAxis == yAxis) {
				return var;
			}
        }
		return QSharedPointer<PlotData>();
    };
signals:
    //选择状态改变信号
    void chekStatusChanged(QSharedPointer<PlotData>& data);
private slots:
    void show_changed();
public slots:
    void setCurrPlotData(const QSharedPointer<PlotData>&) ;
    void setCurrPlotData(int index);
public:
    const QList<QSharedPointer< PlotData>> & data() { return _data; }

     QSharedPointer< PlotData> current() { 
         if (_curr_index < 0 || _curr_index >= _data.size()) {
             return QSharedPointer< PlotData>();
         }
        else return _data[_curr_index];
    }
private :
    QList<QSharedPointer< PlotData>> _data;
    int _curr_index =  -1 ;
};

class PlotModel : public QAbstractTableModel
{
    Q_OBJECT
public:
    explicit PlotModel(QObject* parent = nullptr);
    ~PlotModel();
private:
    int rowCount(const QModelIndex& parent = QModelIndex()) const override;
    int columnCount(const QModelIndex& parent = QModelIndex()) const override;

    QVariant data(const QModelIndex& index, int role) const override;
    QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;

    bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override;

    Qt::ItemFlags flags(const QModelIndex& index) const override;
signals:
    void chekStatusChanged(QSharedPointer<PlotData>& data);
public slots:
    void add_plotData(QSharedPointer<QCPGraphDataContainer>& data, QString plot_name = "plot");

private:
    QSharedPointer<PlotTreeRootItem> _rootItems;//数据
    QStringList _header;
    PlotData* _curr{ nullptr };
public:
     QSharedPointer<PlotTreeRootItem>& rootItems() { return _rootItems; }
};
